package com.yc.springboot.favorite.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.springboot.favorite.entity.Tag;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 凯
 * @since 2025-08-16
 */
public interface TagMapper extends BaseMapper<Tag> {

    @Select("SELECT\n" +
            "    * \n" +
            "FROM\n" +
            "    tag a \n" +
            "WHERE\n" +
            "    EXISTS (\n" +
            "    SELECT\n" +
            "        * \n" +
            "    FROM\n" +
            "        tag_fav b\n" +
            "        JOIN fav c ON b.fid = c.id \n" +
            "    WHERE\n" +
            "    a.id = b.tid \n" +
            "    AND c.uid = #{uid})")
    List<Tag> selectByUid(Integer uid);


    @Select("select tag.*, #{uid} uid from tag where  id = #{id}")
    // <resultMap><id><result><collection></collection>
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "{tid=id,uid=uid}", property = "favs",
                    //one = 一对一  many = 一对多
                    many = @Many(select = "com.yc.favorite.mapper.FavMapper.queryById"))
    })
    Tag queryById(@Param("id") int id, @Param("uid") int uid);

    @Select("SELECT\n" +
            "\tcount(*) \n" +
            "FROM\n" +
            "\tfav \n" +
            "WHERE\n" +
            "\tuid = #{uid} \n" +
            "\tAND tags LIKE \"%#{name}%\";")
    int queryCount(@Param("uid") int uid,@Param("name") String name);


}
